/* 
 * File:   DrawLogic.cpp
 * Author: RedEyedKiller
 * 
 * Created on 22 Σεπτέμβριος 2010, 7:17 μμ
 */

#include "DrawLogic.h"
#include "Entity.h"
#include "Camera.h"
#include "sdl/GraphicsCore.h"

namespace EntitySystem
{

DrawLogic::DrawLogic()
{
    this->parent = NULL;
}

DrawLogic::DrawLogic(const DrawLogic& orig):Drawable(orig)
{
    this->parent = NULL;
}

DrawLogic::~DrawLogic()
{
}

bool DrawLogic::IsVisible(const Camera& camera) const
{
    if (!m_visible) return false;
    int posX = parent->GetPositionX();
    int posY = parent->GetPositionY();
    return (camera.GetRect()->GetLeft() <= posX + this->GetWidth() * GetScale().GetX() &&
            camera.GetRect()->GetTotalWidth() >= posX) &&
            (camera.GetRect()->GetTop() <= posY + this->GetHeight() * GetScale().GetY() &&
            camera.GetRect()->GetTotalHeight() >= posY);
}

void DrawLogic::Draw(sdl::GraphicsCore *gCore, Camera *camera)
{
    if (m_display != NULL)
    {
        gCore->Render(*m_display->GetRawData(),m_clip,m_color,parent->GetPosition(),m_scale,m_angle,m_flip,m_blending);
    }
}

Math::Vector2I DrawLogic::GetDisplayDimensions()
{
    return Math::Vector2I((int)m_display->GetRawData()->GetWidth(), (int)m_display->GetRawData()->GetHeight());
}

int DrawLogic::GetX() const
{
    return parent->GetPositionX();
}

void DrawLogic::SetX(int newX)
{
    parent->SetPosition(newX, parent->GetPositionY());
}

int DrawLogic::GetY() const
{
    return parent->GetPositionY();
}

void DrawLogic::SetY(int newY)
{
    parent->SetPosition(parent->GetPositionX(), newY);
}

Math::Vector2F DrawLogic::GetPosition() const
{
    return parent->GetPosition();
}

void DrawLogic::SetPosition(const Math::Vector2F& position)
{
    parent->SetPosition(position);
}

DrawLogic* DrawLogic::Clone()
{
    return new DrawLogic(*this);
}

int DrawLogic::GetWidth() const
{
    return m_display ? this->m_display->GetRawData()->GetWidth() : 0;
}

int DrawLogic::GetHeight() const
{
    return m_display ? this->m_display->GetRawData()->GetHeight() : 0;
}

}
